package com.ruoyi.system.service.module.splitAccount.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.module.mer.MerUser;
import com.ruoyi.system.domain.module.splitAccount.SplitAccount;
import com.ruoyi.system.mapper.module.mer.MerUserMapper;
import com.ruoyi.system.mapper.module.splitAccount.SplitAccountMapper;
import com.ruoyi.system.service.module.splitAccount.ISplitAccountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 分成账户Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-08
 */
@Service
public class SplitAccountServiceImpl extends ServiceImpl<SplitAccountMapper, SplitAccount> implements ISplitAccountService {

	@Resource
	private SplitAccountMapper splitAccountMapper;
	@Resource
	private MerUserMapper merUserMapper;

	/**
	 * 账户类型  0 - 平台   1 - 物业  2 -  小区  3 - 商家   4 - 个人
	 * @param userId
	 * @param accountType
	 * @return
	 */
	@Override
	public SplitAccount getAccountByUserId(int userId, int accountType, int accountId) {
		QueryWrapper<SplitAccount> qw = new QueryWrapper<>();
		qw.eq("user_id",userId);
		qw.eq("account_type",accountType);
		qw.eq("account_id",accountId);
		qw.last("limit 1");
		return splitAccountMapper.selectOne(qw);
	}

	/**
	 * 账户类型  0 - 平台   1 - 物业  2 -  小区  3 - 商家   4 - 个人
	 * @param merId
	 * @return
	 */
	@Override
	public SplitAccount getAdminAccountByType(int merId,int accountType) {
		QueryWrapper<MerUser> qwMerUser = new QueryWrapper<>();
		qwMerUser.eq("mer_id",merId);
		qwMerUser.eq("is_admin",1);
		qwMerUser.last("limit 1");
		MerUser merUser = merUserMapper.selectOne(qwMerUser);
		if(merUser != null){
			QueryWrapper<SplitAccount> qw = new QueryWrapper<>();
			qw.eq("user_id", merUser.getUserId());
			qw.eq("account_id", merId);
			qw.eq("account_type",accountType);
			qw.last("limit 1");
			return splitAccountMapper.selectOne(qw);
		}
		return null;
	}
}
